var isGetH = true, isGetP = true, isGetTD = false, isGetSpan = false;
if(window.location.href.indexOf('https://www.chinafix.com/thread-') != -1){
	isGetH = false, isGetP = false, isGetTD = true;
}

var s = '<html>\n<head>\n<title>' + document.title + '</title>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>\n<style>\nbody { background:#eeeeee; }\nh1, h2, h3, h4 { text-align:center; }\np { text-indent:2em; font-size:1.5em;}\nimg { display:block; margin:0 auto; max-width:100%;}\na { text-decoration:none; }\n#content { width:1000px; margin:2em auto; padding:2em; background:#ffffff; border-radius:10px; }\n</style>\n</head>\n<body>\n<div id="content">';

if (location.href.indexOf('https://baijiahao.baidu.com/s?id=') == 0) {
    s = s + '\n<a href="' + location.href + '" target=_blank>' + document.getElementsByClassName('article-title')[0].innerHTML + '</a>\n<h3>' + document.getElementsByClassName('author-name')[0].innerHTML + '</h3>\n' + document.getElementsByClassName('article-content')[0].innerHTML.replace(/<\/?span.*?>/gi, '').replace(/<\/?div.*?>/gi, '').replace(/<([a-z]+?)(?:\s+?[^>]*?)?>\s*?<\/\1>/gi, '').replace(/<img[^>]*?(src="[^"]*?")[^>]*?>/gi, '<img $1>'); //去span，去div，去空tag，清洁img
}else{

if(isGetH){
var h1s = document.getElementsByTagName('h1');
for(i=0; i<h1s.length; i++){
	s = s + '\n<h1><a href="' + window.location.href + '">' + h1s[i].innerHTML.replace(/^\s+|\s+$/g,'') + '</a></h1>';
}

var h2s = document.getElementsByTagName('h2');
for(i=0; i<h2s.length; i++){
	s = s + '\n<h2><a href="' + window.location.href + '">' + h2s[i].innerHTML.replace(/^\s+|\s+$/g,'') + '</a></h2>';
}

var h3s = document.getElementsByTagName('h3');
for(i=0; i<h3s.length; i++){
	s = s + '\n<h3><a href="' + window.location.href + '">' + h3s[i].innerHTML.replace(/^\s+|\s+$/g,'') + '</a></h3>';
}

var h4s = document.getElementsByTagName('h4');
for(i=0; i<h4s.length; i++){
	s = s + '\n<h4><a href="' + window.location.href + '">' + h4s[i].innerHTML.replace(/^\s+|\s+$/g,'') + '</a></h4>';
}
}


if(isGetP){
var ps = document.getElementsByTagName('p');
console.log('p[' + ps.length + ']');
for(i=0; i<ps.length; i++){
	//if(ps[i].innerHTML.indexOf('<script') == -1){
		//var pc = ps[i].innerHTML.replace(/<\/?span.*?>/g, '').replace(/style\s*?=\s*?(['"])[\s\S]*?\1/gi, '').replace(/<\/?font[^>]*>/gi, '').replace(/<\/?o:p[^>]*>/gi, '').replace(/&nbsp;/ig, '').replace(/^\s+|\s+$/g, ''); 		//去span, 首尾空格replace(/^\s+|\s+$/g, '')，去style，去img replace(/<img.*?(?:>|\/>)/gi, '')
		var pc = ps[i].innerHTML;//.replace(/&nbsp;/ig, '')
		pc = pc.replace(/<\/?span.*?>/g, '');
		if(pc != '<br>' && pc != '' && pc != ' ' && pc != ' \n')
			s = s + '\n<p>' + pc + '</p>';	
	//}
}
}

if(isGetSpan){
var spans = document.getElementsByTagName('span');
for(i=0; i<spans.length; i++){
	var sc = spans[i].innerHTML;
	if(sc != '&nbsp;')
		s = s + '\n<p>' + spans[i].innerHTML + '</p>';	
}
}

/* 有跨域问题
var iframes = document.getElementsByTagName('iframe');
console.log(iframes);
for(i=0; i<iframes.length; i++){
	var FD = iframes[i].contentWindow.document;
	if(FD != null){
		ps = FD.getElementsByTagName('p');
		for(j=0; j<ps.length; j++){
			if(ps[j].innerHTML.indexOf('<script') == -1)
				s = s + '\n<p>' + ps[j].innerHTML.replace(/^\s+|\s+$/g,'') + '</p>';
		}
	}
}
*/

if(isGetTD){
var tds = document.getElementsByTagName('td');
for(i=0; i<tds.length; i++){
	if(tds[i].id.indexOf('postmessage_') != -1){
		var tdc = tds[i].innerHTML;
		console.clear();
		tdc.replace(/<img.*zoomfile=['"]([^'"]+).*>/gi, function() {
			// arguments[]: 匹配到的字符串，匹配到的N个子字符串，匹配到的字符串位置，原字符串
			console.log(arguments);		
		});
		tdc = tdc.replace(/<i.*>(.*)<\/i>/g, '$1');
		tdc = tdc.replace(/<\/?font.*?>/g, '');
		tdc = tdc.replace(/<\/?ignore_js_op>/g, '');
		//tdc = tdc.replace(/<div([\s\S]*?)<\/div>/g, '');	//还有一个</div>没有删除
		tdc = tdc.replace(/<div[\s\S]*?<\/div>/g, '');
		tdc = tdc.replace(/<\/div>/g, '');	//还有一个</div>没有删除
		tdc = tdc.replace(/<img.*zoomfile=['"]([^'"]+).*>/gi, '<img src="$1">');	//img 的 zoomfile 替换 src
		tdc = tdc.replace(/<img.*src=['"]([^'"]+).*>/gi, '<img src="$1">');	//清洁img
		tdc = tdc.replace(/\n(.+)<br>/gi, '\n<p>$1</p>\n');	// *<br> 替换成 <p>*</p>
		tdc = tdc.replace(/<br>/gi, '');
		s = s + tdc;
	}
}
}

}

s += '\n</div>\n</body>\n</html>';
var blob = new Blob([s], { 'type': 'text/html' });
var url = URL.createObjectURL(blob);
window.open(url);